package com.jadesoft.stock.rp;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.util.Assert;

import com.jadesoft.common.web.AjaxAction;
import com.jadesoft.common.web.Bind;
import com.jadesoft.stock.FetionHelper;
import com.jadesoft.stock.UserBS;
import com.jadesoft.stock.UserBSImpl;
import com.jadesoft.stock.entity.User;
import com.jadesoft.stock.util.Constants;

public class UserAction extends AjaxAction {
	private UserBS userBS = new UserBSImpl();
	private Logger logger = Logger.getLogger(UserAction.class);

	/**
	 * @param user
	 * @return
	 */
	public boolean check(User user) {
		Assert.hasText(user.getEmail(), "邮箱不能为空!");
		return userBS.find(user) == null;
	}

	/**
	 * true:注册成功 false:注册失败
	 * 
	 * @param user
	 * @return
	 */
	public boolean register(User user) {
		Assert.hasText(user.getEmail(), "邮箱不能为空!");
		Assert.hasText(user.getPassword(), "密码不能为空!");
		user.setState(User.STATE_REGISTED);
		try {
			userBS.registerTA(user);
			return true;
		} catch (EmailException e) {
			this.logger.error(e);
			return false;
		}
	}

	/**
	 * 激活账号
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward active(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setContentType("text/html;charset=utf-8");
		String email = request.getParameter("email");
		String code = request.getParameter("code");
		if (StringUtils.isBlank(email) || StringUtils.isBlank(code)) {
			response.sendRedirect(Constants.HTML_ERROR + "?msg="
					+ Constants.ERROR_A001);
			return null;
		}
		User user = userBS.find(new User(email));
		if (user == null) {
			response.sendRedirect(Constants.HTML_ERROR + "?msg="
					+ Constants.ERROR_A002);
			return null;
		}
		
		user.setState(User.STATE_ACTIVED);
		userBS.activeTA(user);
		response.sendRedirect(Constants.HTML_SUCCESS);
		return null;
	}
	
	/**
	 * 登陆
	 */
	public String login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setContentType("text/html;charset=utf-8");
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		if (StringUtils.isBlank(email) || StringUtils.isBlank(password)) {
			return Constants.ERROR_A001;
		}
		User user = userBS.find(new User(email,password));
		if (user == null) {
			return Constants.ERROR_A003;
		}
		if(user.getState().equals(User.STATE_REGISTED)){
			return Constants.ERROR_A004;
		}else if(user.getState().equals(User.STATE_FREEZED)){
			return Constants.ERROR_A005;
		}
		request.getSession().setAttribute(Constants.LONGIN_USER, user);
		return StringUtils.isBlank(user.getMobileNo()) ? Constants.LOGIN_SUCCESS_1:Constants.LOGIN_SUCCESS;
	}
	
	//更新用户信息
	public boolean update(User user)  {
		Assert.hasText(user.getName(), "姓名不能为空!");
		Assert.hasText(user.getMobileNo(), "手机号不能为空!");
		User userBo = this.getUser();
		userBo.setBirthday(user.getBirthday());
		userBo.setSex(user.getSex());
		userBo.setMobileNo(user.getMobileNo());
		userBo.setName(user.getName());
		userBS.save(userBo);
		try {
			
			FetionHelper.addBuddy(user.getMobileNo());//添加好友
		} catch (Exception e) {
			log.error(e);
		}
		return true;
	}
	
	//获取用户基本信息
	public User loadUserInfo(){
		return this.getUser();
	}
	
	/**
	 * 安全退出
	 */
	public String logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setContentType("text/html;charset=utf-8");
		request.getSession().invalidate();
		response.sendRedirect(Constants.LOGIN_PAGE);
		return null;
	}
	
	/**
	 * 修改密码
	 * @param pswd
	 * @return
	 */
	public boolean updatePswd(@Bind(key = "pswd") String pswd ){
		Assert.hasText(pswd, "新密码不能为空!");
		User user = this.getUser();
		user.setPassword(pswd);
		userBS.save(user);
		return true;
	} 
}
